package com.woniuxy.student.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

public class WebSocketUtil {
	//存放所有在聊天室的客户端的session
public static final Map<Integer, Session>users=new ConcurrentHashMap<Integer, Session>();
//给某人发消息
public static void sendMessage(int key,String message) {
    Session session=users.get(key);
	//获取发送信息的工具类对象
	RemoteEndpoint.Basic basic=session.getBasicRemote();
	//连接有效时
	if (basic!=null) {
		try {
			basic.sendText(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
//public static void sendAll(String message) {
//	Iterator<Integer> 	iterator=users.keySet().iterator();
//	while (iterator.hasNext()) {
//        Integer key= (Integer) iterator.next();
//		Session session=users.get(key);
//		RemoteEndpoint.Basic basic=session.getBasicRemote();
//		try {
//			basic.sendText(message);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}
//}

}
